<?php
namespace app\index\controller;
use think\Request;
use think\Db;
use think\Controller;
use think\cache\driver\Redis;
use think\cache\driver\RedisHash;

class Index extends Controller
{
    public function _initialize()
    {
        parent::_initialize();
        $this->redishash = RedisHash::instance();
        $this->redis = new Redis();
        
    }
    public function index()
    {
        $userinfo = Db::table('user')->select();
        if($userinfo){
        	$code = 200;
        	$msg = '查询成功';
        }else{
        	$code = 400;
        	$msg = '查询失败';
        }
        $data = array('code'=>$code,
        	          'msg'=>$msg,
        	          'userinfo'=>$userinfo
        	         );
        return json_encode($data);
    }

    public function category()
    {
       $category = Db::table('category')->where(['level'=>1,'parentid'=>0])->order('displayorder asc')->select();
          foreach ($category as $k => $v) {
            $childern_category = Db::table('category')->where('parentid',$v['id'])->order('displayorder asc')->select();
            $category[$k]['children_category'] = $childern_category;
       }
      //$this->redishash->setHashKey("category:1")->set(array('category'=>json_encode($category)));
      //$this->redishash->setHashKey("category:1")->set(array('category'=>json_encode($category)));
      // $this->redishash->setTable("category")->setKey("2")->set(array('name'=>'tom','id'=>2));
      // $this->redishash->setTable("category")->setKey("3")->set(array('name'=>'sev','id'=>3));
      //获取方式  整条数据
      //$this->redishash->setTable("category")->setKey(1)->get();
      // echo "info_2完整数据".PHP_EOL;
      // dump($this->redishash->setHashKey("category:2")->get());
      // echo "info_3完整数据".PHP_EOL;
      // dump($this->redishash->setHashKey("category:3")->get());

      //获取方式  指定字段
      // echo "info_1指定字段".PHP_EOL;
      // dump($this->redishash->setTable("category")->setKey("1")->get(["id","name"]));
      // echo "info_2指定字段".PHP_EOL;
      // dump($this->redishash->setHashKey("category:2")->get("id,name"));
      // echo "info_3指定字段".PHP_EOL;
      // dump($this->redishash->setHashKey("category:3")->get("name"));

    	if($category){
        	$code = 200;
        	$msg = '查询成功';
        }else{
        	$code = 400;
        	$msg = '查询失败';
        }
        $data = array('code'=>$code,
        	          'msg'=>$msg,
        	          'category'=>$category
        	         );
        return json_encode($data);
    }

    public function city()
    {
        $getinfo = Request::instance()->get();
        $keywords = $getinfo['keywords']?$getinfo['keywords']:'';
        $limit = $getinfo['limit']?$getinfo['limit']:1;
        $city = Db::table('areas')->where('name','like','%'.$keywords.'%')->limit($limit)->select();
        if($city){
           $code = 200;
           $msg = '查询成功'; 
        }else{
           $code = 400;
           $msg = '查询失败';
        }
        $data = array('code'=>$code,
                      'msg'=>$msg,
                      'city'=>$city
                     );
        return json_encode($data);
    }

    public function worker()
    {
      $postinfo = Request::instance()->post();
      $data = ['name'=>$postinfo['name'],'tel'=>$postinfo['tel'],'icon'=>$postinfo['icon'],'worker_type'=>$postinfo['worker_type'],'worker_age'=>$postinfo['worker_age'],'id_card'=>$postinfo['id_card'],'remark'=>$postinfo['remark'],'createtime'=>time()];
      $result = Db::name('worker')->data($data)->insert();
      if($result){
        $code = 200;
        $msg = '新增成功';
      }else{
        $code = 400;
        $msg = '新增失败';
      }
      $info = array('code'=>$code,'msg'=>$msg);
      return json_encode($info);
    }

    public function floor_category()
    {
      $getinfo = Request::instance()->get();
      $cateid = $getinfo['cateid'];
      $page = $getinfo['page']?$getinfo['page']:1;
      $pagesize = $getinfo['pagesize']?$getinfo['pagesize']:10;
      $floor = Db::name('floor')->where('cateid',$cateid)->order('displayorder asc')->limit(($page-1)*$pagesize,$pagesize)->select();
      $count = count(Db::name('floor')->where('cateid',$cateid)->select());
      if($floor){
        $code = 200;
        $msg = '查询成功';
      }else{
        $code = 400;
        $msg = '查询失败';
      }
      $data = array('code'=>$code,'msg'=>$msg,'count'=>$count,'floor'=>$floor);
      return json_encode($data);
    }

    public function redis_test()
    {
      // try {
      //     //开启事务
      //     $this->redis->multi();
      //     $this->redis->setex('keyTest',60,1);
      //     $this->redis->get('keyTest');
      //     $this->redis->inc('keyTest');
      //     $this->redis->get('keyTest');
      //     //执行事务
      //     $ret = $this->redis->exec();
      //     print_r($ret);
      // } catch (Exception $e){
      //     echo $e->getMessage();
      // }

      // try {
      //     //先设置缓存keyTest为1
      //     $this->redis->setex('keyTest', 60, 1);
      //     //开启事务
      //     $this->redis->multi();
      //     $this->redis->setex('keyTest', 60, 10);
      //     $this->redis->get('keyTest');
      //     $this->redis->inc('keyTest');
      //     $this->redis->get('keyTest');
      //     //取消事务
      //     $this->redis->discard();
      //     $ret = $this->redis->get('keyTest');
      //     var_dump($ret);
      //     //查看keyTest
      // } catch (Exception $e){
      //     echo $e->getMessage();
      // }

      try {
          //先设置缓存keyTest为1
          $this->redis->setex('keyTest', 60, 1);
          //监视keyTest
          $this->redis->watch(array('keyTest'));
          //假设在开始监视之后，执行事务之前，keyTest被并发操作redis的其他用户修改了
          $this->redis->setex('keyTest', 60, 10);
          //开启事务
          $this->redis->multi();
          $this->redis->inc('keyTest');
          //执行事务
          $ret = $this->redis->exec();
          var_dump($ret);
          $ret = $this->redis->get('keyTest');
          var_dump($ret);
          //查看keyTest
      } catch (Exception $e){
          echo $e->getMessage();
      }
    }
}
